package com.makeunion.gdlbsdemo.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;

import com.amap.api.maps.AMap;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.animation.Animation;
import com.amap.api.maps.model.animation.RotateAnimation;
import com.makeunion.gdlbsdemo.R;

/**
 * 类说明
 *
 * @author renjialiang
 * @version [版本]
 * @see [相关类]
 * @since [模块]
 */
public class MarkPointActivity extends AppCompatActivity {

    private MapView mapView;

    private AMap aMap;

    private Marker marker;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mark_point);

        mapView = (MapView) findViewById(R.id.map_view);
        mapView.onCreate(savedInstanceState); //此方法必须重写
        init();
    }

    private void init() {
        if (aMap == null) {
            aMap = mapView.getMap();
        }

        LatLng latLng = new LatLng(40d,116.397972);
        MarkerOptions markerOptions = new MarkerOptions().position(latLng)
                .title("北京")
                .snippet("简单描述")
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.custom_location_icon))
                .draggable(true)
                .visible(false)
                .anchor(0.5f, 1f)
                .alpha(0.8f);
        marker = aMap.addMarker(markerOptions);

        aMap.setOnMarkerClickListener(new AMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                Log.e("TAG", "onMarkerClick:" + marker.getTitle());
                return false;
            }
        });

        aMap.setOnMarkerDragListener(new AMap.OnMarkerDragListener() {
            @Override
            public void onMarkerDragStart(Marker marker) {
                Log.e("TAG", "onMarkerDragStart:" + marker.getTitle());
            }

            @Override
            public void onMarkerDrag(Marker marker) {
                Log.e("TAG", "onMarkerDrag:" + marker.getTitle());
            }

            @Override
            public void onMarkerDragEnd(Marker marker) {
                Log.e("TAG", "onMarkerDragEnd:" + marker.getTitle());
            }
        });
    }

    public void addMarker(View view) {
        marker.setVisible(!marker.isVisible());
    }

    public void showInfoWindow(View view) {
        if (marker.isVisible()) {
            marker.showInfoWindow();
        } else {
            marker.hideInfoWindow();
        }
    }

    public void startAnim(View view) {
        Animation animation = new RotateAnimation(marker.getRotateAngle(), marker.getRotateAngle() + 360, 0, 0, 0);
        animation.setDuration(1000);
        animation.setInterpolator(new LinearInterpolator());
        marker.setAnimation(animation);
        marker.startAnimation();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }
}
